package mazeMultiThread;

//This is the main one - Code Tester

//import java.awt.event.MouseListener;
//import java.util.Scanner;

//import java.util.Scanner;



import javax.swing.*;

/**
 * Main class
 * @author Dani
 *
 */
public class Ventana
{

    private static JFrame ventana = new JFrame("Maze generator");
    private static MazeContainer mazeContainer = new MazeContainer(GH.MAZE_WIDTH,GH.MAZE_HEIGHT,ventana);
    private static Interpreter interpreter = new Interpreter();
    private static VentanaMouseListener gsml = new VentanaMouseListener();
    private static VentanaKeyboardListener vkbl = new VentanaKeyboardListener();
    
    //private int CURRENT_WIDTH = 600;
    //private int CURRENT_HEIGHT = 400;
    private static final int MAX_FPS = 60;
    private static int framesLastInterval = 0;
    private static long lastCheckForFrames = 0;
   
    
    
    public static void main(String[] args)
    {
    	//initialize the window setting various parameters
        ventana.setSize(GH.VENTANA_WIDTH,GH.VENTANA_HEIGHT); // Size of Window
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Allows the program to end when click exit
        ventana.setResizable(true); // User cannot resize the window
       
        //add the maze Container to the window, that's what will get rendered
        ventana.add(mazeContainer); 
        //add mouse and keyboard listeners to capture input events
        ventana.addMouseListener(gsml);
        ventana.addMouseWheelListener(gsml);
        ventana.addKeyListener(vkbl);
        //initialize the keyboard interpreter in a new thread for controlling console commands which won't cause keyboard events
        interpreter.start();
        
        //set the visibility of the window so it is visible after all initializations
        ventana.setVisible(true);
        

        while (true)
        {
        	//sleep for an interval which will cause the while to loop MAX_FPS times per second, potentially redrawing the maze if possible
        	try {
				Thread.sleep(1000/MAX_FPS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            
        	
        	//each second, refresh the current FPS with the new data
        	long interval;
        	if ((interval = (System.currentTimeMillis()-lastCheckForFrames)) > 1000) {
        		//System.out.println("Running at: " + (float)((int)(100*framesLastInterval/(interval/1000.0)))/100 + " FPS / " + MAX_FPS);
        		lastCheckForFrames = System.currentTimeMillis();
        		//reformatting of the value is necessary to avoid more than 2 decimal places when displayed
        		mazeContainer.setFrames((float)((int)(100*framesLastInterval/(interval/1000.0)))/100);
        		framesLastInterval = 0;
        	}
        	//if mazeContainer is fully painted, increase frame count. Due to GPU limitations this is necessary to avoid unnecessary calls to the repaint method
        	if (GH.isPainted) {
        		GH.isPainted = false;
        		mazeContainer.repaint();
        		framesLastInterval++;
        	}
            
        }

    }
}
